import os
import ConfigParser
from utils import gen_maple_xml 
from utils import gen_zone_file
from utils import gen_zone_xml
from utils import add_zone_to_conf
from utils import gen_named_conf
from utils import gen_view_info 
from utils import gen_acl_xml
from perf_decorater import time_decorater

QUERY_FILE = 'qp_file.txt'

class Base_conf():
    def __init__(self, server, port, mainpath):
        self.server = server
        self.port = port
        self.mainpath = mainpath

    def remove_old_data(self, filename):
        os.system("rm -rf " + self.mainpath + '/data')
        os.system("rm -rf " + self.mainpath + '/etc')
        os.system("rm -rf " + self.mainpath + '/log')
        os.system("rm -rf " + self.mainpath + '/' + filename)
    def tar_file(self, filename):
        currentpath = os.getcwd()
        os.chdir(self.mainpath)
        cmd = 'tar zcvf ' + filename + ' etc data log > /dev/null'
        os.system(cmd)
        os.chdir(currentpath)

class Bind_conf(Base_conf):
    @time_decorater
    def gen_conf(self, opts):
        opts['conf'] = self.mainpath
        opts['svr'] = self.server + ':' + str(self.port)
        gen_named_conf.main(opts)
        self._gen_zone_file(opts)

    def _gen_zone_file(self, opts):
        opts['path'] = self.mainpath + '/data'
        gen_zone_file.main(opts)
        if 'viewnum' in opts and opts['viewnum'] != 0:
            viewlist = gen_view_info.gen_rand_view(opts['viewnum'])
            for view in viewlist:
                os.system("mkdir -p " + os.path.join(opts['path'], view))
                os.system("cp "+ opts['path'] + '/*.txt  ' + os.path.join(opts['path'], view))
            os.system("rm -f " + opts['path'] + '/*.txt')

    def additional_op(self, opts):
        pass
class Maple_conf(Base_conf):
    @time_decorater
    def gen_conf(self, opts):
        opts['conf'] = self.mainpath
        opts['svr'] = self.server + ':' + str(self.port)
        gen_maple_xml.main(opts)

        self._gen_zone_xml(opts)
        self._gen_zone_file(opts)

    def _gen_zone_xml(self, opts):
        opts['aclpath'] = self.mainpath + '/etc'
        opts['outfile'] = self.mainpath +'/etc/zones.xml'
        gen_zone_xml.main(opts)
        
        gen_acl_xml.main(opts)
    def _gen_zone_file(self, opts):
        opts['path'] = self.mainpath + '/data'
        gen_zone_file.main(opts)
        os.system("cp %s/utils/add_zone_to_conf.py %s/etc" % ( os.getcwd(), self.mainpath));
        if 'viewnum' in opts and opts['viewnum'] != 0:
            viewlist = gen_view_info.gen_rand_view(opts['viewnum'])
            for view in viewlist:
                os.system("mkdir -p " + os.path.join(opts['path'], view))
                os.system("cp "+ opts['path'] + '/*.txt  ' + os.path.join(opts['path'], view))
            os.system("rm -f " + opts['path'] + '/*.txt')

    def _add_zone_to_conf(self, opts):
        opts['conf'] = self.mainpath + '/etc/maple.xml'
        opts['path'] = self.mainpath + '/data'
        add_zone_to_conf.main(opts)

    def additional_op(self, opts):
        pass
